package com.example.day3_noti;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.NotificationTarget;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void normal(View view) {
       /* //获取通知管理器
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //实例化通知对象
        Notification notification = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)//小图标  必须设置不让报错
                .setContentTitle("我是普通通知的标题")
                .setContentText("我是普通通知的内容")
                .setAutoCancel(true)//点击自动取消
                .build();
        //发送通知
        manager.notify(1,notification);*/
        //版本控制
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, MainActivity.class);
        //参数一上下文  参数二 请求码自行定义 不要重复  参数三 跳转到的activity的intent  参数四 常量值 用UPDATE
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)//小图标  必须设置不让报错
                .setContentTitle("我是普通通知的标题")
                .setContentText("我是普通通知的内容")
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);//点击自动取消
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            //安卓9.0及以上
            manager.createNotificationChannel(new NotificationChannel("channelId","channelName",NotificationManager.IMPORTANCE_DEFAULT));
            builder.setChannelId("channelId");
        }
        manager.notify(1,builder.build());
    }
    //分组通知  给所有的通知对象设置属性Group,给组名相同的任意一个通知设置GroupSummary为true
    public void group(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this)
                .setGroup("a")
                .setGroupSummary(true)//设置后，此条通知就看不见了，其他得到通知会在此上方
                .setSmallIcon(R.mipmap.ic_launcher)//小图标  必须设置不让报错
                .setContentTitle("我是普通通知的标题")
                .setContentText("我是普通通知的内容")
                .setAutoCancel(true);//点击自动取消
        Notification.Builder builder1 = new Notification.Builder(this)
                .setGroup("a")
                .setSmallIcon(R.mipmap.ic_launcher)//小图标  必须设置不让报错
                .setContentTitle("我是普通通知的标题1")
                .setContentText("我是普通通知的内容1")
                .setAutoCancel(true);//点击自动取消
        Notification.Builder builder2 = new Notification.Builder(this)
                .setGroup("a")
                .setSmallIcon(R.mipmap.ic_launcher)//小图标  必须设置不让报错
                .setContentTitle("我是普通通知的标题2")
                .setContentText("我是普通通知的内容2")
                .setAutoCancel(true);//点击自动取消
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            //安卓9.0及以上
            manager.createNotificationChannel(new NotificationChannel("channelId","channelName",NotificationManager.IMPORTANCE_DEFAULT));
            builder.setChannelId("channelId");
            builder1.setChannelId("channelId");
            builder2.setChannelId("channelId");
        }
        manager.notify(1,builder.build());
        manager.notify(2,builder1.build());
        manager.notify(3,builder2.build());
    }
    Timer timer;
    public void progress(View view) {
        NotificationManager service = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("下载")
                .setContentText("正在下载。。。。")
                .setProgress(100, 0, false);
       /* if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            service.createNotificationChannel(new NotificationChannel("id","name",NotificationManager.IMPORTANCE_DEFAULT));
            builder.setChannelId("id");
        }*/
        timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress = 0;
            @Override
            public void run() {
                if(progress==100){
                    //定时器关闭，通知关闭
                    timer.cancel();
                    service.cancel(5);
                }else{
                    builder.setProgress(100,progress+=20,false);
                    service.notify(5,builder.build());
                }

            }
        },0,1000);
    }

    public void diy(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        @SuppressLint("RemoteViewLayout") RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout_noti);
        views.setTextViewText(R.id.noti_tv,"张三李四");
        //views.setImageViewResource(R.id.noti_iv,R.drawable.ic_play);
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("云音乐")
                .setCustomContentView(views);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
            manager.createNotificationChannel(new NotificationChannel("id","name",NotificationManager.IMPORTANCE_DEFAULT));
            builder.setChannelId("id");
        }
        Notification notification = builder.build();
        manager.notify(1,notification);
        //参数一 上下文  参数二 图片的id  参数三 生成的布局文件remoteViews 参数四 通知对象  参数五通知的id  必须和发送的id 相同
        NotificationTarget notificationTarget = new NotificationTarget(this, R.id.noti_iv, views, notification, 1);
        Glide.with(this)
                .asBitmap()
                .load("https://developer.huawei.com/allianceCmsResource/resource/HUAWEI_Developer_VUE/images/homeNew/banner4-1124.jpg")
                .into(notificationTarget);

    }
    //双击退出APP
    long lastTime=0l;//上一次的点击时间
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){//是否是返回键
            //对比第二次的时间和第一次点击的时间差 是否大于2秒 ，大于两秒给出对应的提示，不退出App   小于两秒认为是连续点击了两次，退出APP
            long time = System.currentTimeMillis();
            if(time-lastTime>2000){
                Toast.makeText(this, "再按一次，退出APP", Toast.LENGTH_SHORT).show();
                //更新上次点击时间，方便和下次点击作对比
                lastTime = time;
                return true;//不退APP
            }
        }
        return super.onKeyDown(keyCode, event);//退出App
    }
}